<?php

namespace Biz\service\BrandRights;


use Biz\model\brandRights\BrandRightsSource;
use Doraemon\pockets\datebase\BaseDbModel;

class BrandRightsSourceService
{
    /**
     * 根据条件查询列表
     * @param $where
     * @param string $field
     * @return array
     */
    public static function getList($where, string $field = "*"): array
    {
        $list = BrandRightsSource::selectList($where, $field);
        $list = array_column($list, null, 'id');
        foreach ($list as &$item) {
            if ($item['child_ids']) {
                $childs = explode(',', $item['child_ids']);
                foreach ($childs as $i) {
                    $item['child'][] = $list[$i]['rights_title'];
                }
            } else {
                $item['child'] = [];
            }
        }
        return $list;
    }


    /**
     * 审核操作
     * @param array $params
     * @return bool
     */
    public static function audit(array $params): bool
    {
        $where['id'] = $params['id'];
        $data['data_status'] = $params['data_status'];
        $data['admin_id'] = $params['admin_id'];
        BrandRightsSource::updateData($data, $where);
        return true;
    }

    /**
     * 根据id集合获取列表
     * @param array $ids
     * @return array
     */
    public static function getListByIds(array $ids): array
    {
        $where['id'] = $ids;
        $where['data_status'] = BrandRightsSource::STATUS_ON;

        $field = ["id", "rights_title"];
        return BrandRightsSource::selectList($where, $field);
    }

    /**
     * 查询所有开启权益
     * @param string $fields
     * @return array
     */
    public static function getAll($fields = '*'): array
    {
        $where['data_status'] = BrandRightsSource::STATUS_ON;
        $where['ORDER'] = ['rights_type' => "ASC"];
        return BrandRightsSource::selectList($where, $fields);
    }


    /**
     * 根据id集合获取列表
     * @param $id
     * @return array
     */
    public static function getListById($id)
    {
        $where['id'] = $id;
        $where['data_status'] = BrandRightsSource::STATUS_ON;
        return BrandRightsSource::getInfo($where);
    }

    /**
     * 获取所有权益类型英文名称作为key（[column]=rights_type）
     * @param string $column
     * @return array
     */
    public static function getNameColumnMap(string $column = 'rights_type'): array
    {
        $sourceList = BrandRightsSource::selectList([], ['rights_name', 'rights_type']);
        if ($sourceList) {
            return array_column($sourceList, $column, 'rights_name');
        }
        return [];
    }


    /**
     * 查询所有权益列表
     * @param $field
     * @return array
     */
    public static function getAllSourceList($field = "*"): array
    {
        $list = BrandRightsSource::selectList([], $field);
        return array_column($list, null, 'rights_name');
    }

    /**
     * 获取权限id和数据的Map映射
     * @param $where
     *
     * @return array
     */
    public static function getRightsSourceMap($where, $column = null)
    {
        $sourceRights = BrandRightsSource::selectList($where);
        if ($sourceRights && is_array($sourceRights)) {
            return array_column($sourceRights, $column, 'id');
        }
        return [];
    }


    /**
     * 获取活动权益列表
     * @param string $field
     * @return array
     */
    public static function getActList($field = "*"): array
    {
        $where['data_status'] = BaseDbModel::STATUS_NORMAL;
        $where['type'] = BrandRightsSource::TYPE_ACT;
        return BrandRightsSource::selectList($where, $field);
    }

    /**
     * 根据权益名称查询权益
     * @param $rightsName
     * @return array
     */
    public static function getOneSource($rightsName): array
    {
        $where['data_status'] = BaseDbModel::STATUS_NORMAL;
        $where['rights_name'] = $rightsName;
        $info = BrandRightsSource::getInfo($where);
        if (is_null($info)) {
            return [];
        }
        return $info;
    }

    /**
     * 查询所有权益列表(包括已删除的)
     * @param array $where
     * @param mixed $field
     * @return array
     */
    public static function getAllSource($field = "*", array $where = []): array
    {
        return BrandRightsSource::selectList($where, $field);
    }

    /**
     * 获取全部权益用id作为键
     * @return array
     */
    public static function getAllSourceKeyById(): array
    {
        $allSourceList = self::getAllSourceList([
            'rights_name',
            'rights_title',
            'rights_type',
            'id'
        ]);
        return $allSourceList ? array_column($allSourceList, null, 'id') : [];
    }


    /**
     * 根据名字获取id
     * @param $rightsName
     * @return int
     */
    public static function getIdByRightsName($rightsName): int
    {
        $where['rights_name'] = $rightsName;
        $id = BrandRightsSource::getInfo($where,'id');
        if (!$id){
            return 0;
        }
        return $id;
    }

    /**
     * 根据名字获取id
     * @param array $rightsNames
     * @return array
     */
    public static function getIdsByRightsNames($rightsNames)
    {
        $rights = [];
        $where['rights_name'] = $rightsNames;
        $result = BrandRightsSource::selectList($where, ['id','rights_name']);
        if ($result && is_array($rights)) {
            $rights = array_column($result, 'id', 'rights_name');
        }
        return $rights;
    }

    /**
     * 根据名字获取id
     * @param $id
     * @return string
     */
    public static function getNameById($id): string
    {
        $where['id'] = $id;
        $rightsName = BrandRightsSource::getInfo($where,'rights_name');
        if (!$rightsName){
            return '';
        }
        return $rightsName;
    }

    /**
     * 根据id获取type
     * @param $id
     * @return string
     */
    public static function getRightsTypeById($id): string
    {
        $where['id'] = $id;
        $rightsType = BrandRightsSource::getInfo($where,'rights_type');
        if (!$rightsType){
            return '';
        }
        return $rightsType;
    }

}